// pages/course/onlineCourse/onlineCourse.js
const app=getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    pageNum:1,
    pageSize:4,
    min:10.00,
    max:50.00,
    courseList:[],
    courseListBycollects:[],
    selectCourseByPriceDesc:[],
    selectCourseByPriceAsc:[],
    selectCourseByPrice:[],
    setInterval:false,
    active:0,
    option1: [
      { text: '价格升序', value: 0 },
      { text: '价格降序', value: 1 },
    ],
    value1: 0,
    courseByPrice:[],
    value2: 'a',
    option2: [
      { text: '免费', value: 'a' },
      { text: '￥50以下', value: 'b' },
      { text: '￥50-100', value: 'c' },
      { text: '￥100-500', value: 'd' },
      { text: '￥500-1000', value: 'e' },
      { text: '￥1000以上', value: 'f' },
    ],
    url: '',
    courseByPriceRange: []
  },
  // 查询所有
selectCourseList(){
  let that = this
  wx.request({
    url: that.data.url+'course/findAll?pageNum='+that.data.pageNum+'&pageSize='+that.data.pageSize,
    success(resp){
      console.log(resp.data.data.list)
      that.setData({
        courseList:resp.data.data.list
      })
      }
  })
},
// 根据收藏量降序排序
selectCourseListBycollects(){
  let that = this
  wx.request({
    url: that.data.url+'course/selectcoursebycollection?pageNum='+that.data.pageNum+'&pageSize='+that.data.pageSize,
    success(resp){
      console.log(resp.data.data.list)
      that.setData({
        courseListBycollects:resp.data.data.list
      })
      }
  })
},
// 根据价格降序
selectCourseByPriceDesc(){
  let that = this
  wx.request({
    url: that.data.url+'course/selectcoursebypricedesc?pageNum='+that.data.pageNum+'&pageSize='+that.data.pageSize,
    success(resp){
      console.log(resp.data.data.list)
      that.setData({
        selectCourseByPriceDesc:resp.data.data.list
      })
      }
  })
},
// 根据价格升序
selectCourseByPriceAsc(){
  let that = this
  wx.request({
    url: that.data.url+'course/selectcoursebypriceasc?pageNum='+that.data.pageNum+'&pageSize='+that.data.pageSize,
    success(resp){
      console.log(resp.data.data.list)
      that.setData({
        selectCourseByPriceAsc:resp.data.data.list,
        courseByPrice: resp.data.data.list
      })
      }
  })
},
onPriceSortChange(e){
  const value = e.detail;
  this.setData({
    value1: value
  });
  console.log(this.data.value1)
  if (value == 0) {
    this.selectCourseByPriceAsc();
    this.setData({
      courseByPrice:this.data.selectCourseByPriceAsc
    })
  } else {
    this.selectCourseByPriceDesc();
    this.setData({
      courseByPrice:this.data.selectCourseByPriceDesc
    })
  }
},
// 根据价格区间
selectCourseByPrice(){
  let that = this;
  let min = 0;
    let max = Infinity;
    switch (this.data.value2) {
      case 'a':
        min = 0;
        max = 0;
        break;
      case 'b':
        min = 0;
        max = 50;
        break;
      case 'c':
        min = 50;
        max = 100;
        break;
      case 'd':
        min = 100;
        max = 500;
        break;
      case 'e':
        min = 500;
        max = 1000;
        break;
      case 'f':
        min = 1000;
        max = 2000000;
        break;
    }
  wx.request({
    url: that.data.url+'course/selectcoursebyprice?pageNum='+that.data.pageNum+'&pageSize='+that.data.pageSize+'&min='+min+'&max='+max,
    success(resp){
      console.log(resp.data.data.list)
      that.setData({
        courseByPriceRange:resp.data.data.list
      })
      }
  })
},
onPriceRangeChange(e) {
  const value = e.detail;
  this.setData({
    value2: value
  });
  this.selectCourseByPrice();
},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // 获取全局的url赋值给当前页面的URL变量
    this.setData({
      url:app.globalData.URL
       })
    // 动态调用后端接口 
    this.selectCourseList();
    this.selectCourseListBycollects();
    this.selectCourseByPriceAsc();
    this.selectCourseByPriceDesc();
    this.selectCourseByPrice();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.setData({
      pageNum: 1,
      pageSize: 4,
      courseList: [],
      courseListBycollects: [],
      selectCourseByPriceDesc: [],
      selectCourseByPriceAsc: [],
      selectCourseByPrice: []
  });
  this.selectCourseList();
  this.selectCourseListBycollects();
  if (this.data.value1 === 1) {
    this.selectCourseByPriceDesc();
} else {
    this.selectCourseByPriceAsc();
}
  this.selectCourseByPrice();
  wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
  let that = this;
  that.setData({
    pageSize:that.data.pageSize+2,
    setInterval:true
  });
  setTimeout(function () {
    that.setData({
      setInterval:false
    });
      }, 2000); 
      this.selectCourseList();
      this.selectCourseListBycollects();
      if (this.data.value1 === 1) {
        this.selectCourseByPriceDesc();
    } else {
        this.selectCourseByPriceAsc();
    }
      this.selectCourseByPrice();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  
  goWisdom(){
    wx.navigateTo({
      url: '/pages/course/wisdomLife/wisdomLife',
    })
  },
  gojingjiangCourse:function(e){
    const id=e.currentTarget.dataset.id;
    console.log(id);
    wx.navigateTo({
     //  url: './pages/course/jingjiangCourse?id='+id
     url:'/pages/course/jingjiangCourse/jingjiangCourse?id='+id
    });
  }
})